﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportService2Ref;
    using WMS6.Web.UserControls;

    public class InOutInvDetail2RptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        private string currFlagAJ = string.Empty;
        private string currFlagOwner = string.Empty;
        private string currFlagReceipt = string.Empty;
        private int deleteCount = 0;
        protected HtmlForm F15346;
        protected GridView gvInOutInvDetailRpt;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        private string lastFlagAJ = string.Empty;
        private string lastFlagOwner = string.Empty;
        private string lastFlagReceipt = string.Empty;
        protected Label lblMsg;
        protected Label lblReceiptInquiryList;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        public void BindData2gvInOutStockDetailList(InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(inOutStockRptDetailQueryEntity.CurrentPage);
            this.gvInOutInvDetailRpt.Visible = true;
            DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetInOutInv2RptDetailDataSet(inOutStockRptDetailQueryEntity);
            base.BindData2GridView(this.gvInOutInvDetailRpt, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity = this.GetInOutStockRptQueryEntity();
            inOutStockRptDetailQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = inOutStockRptDetailQueryEntity.CurrentPage;
            inOutStockRptDetailQueryEntity.CurrentPage = "0";
            using (DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetInOutInv2RptDetailDataSet(inOutStockRptDetailQueryEntity))
            {
                if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
                {
                    base.Export2Excel("InOutInvDetailRpt.xls", "15346", this.gvInOutInvDetailRpt, ds);
                }
            }
            inOutStockRptDetailQueryEntity.IsGetAll = false;
            inOutStockRptDetailQueryEntity.CurrentPage = currentPageIndex;
            this.SetInOutStockDetailRptQueryEntity(inOutStockRptDetailQueryEntity);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity = this.GetInOutStockRptQueryEntity();
            inOutStockRptDetailQueryEntity.CurrentPage = "0";
            this.BindData2gvInOutStockDetailList(inOutStockRptDetailQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity = this.GetInOutStockRptQueryEntity();
            inOutStockRptDetailQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2gvInOutStockDetailList(inOutStockRptDetailQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity = this.GetInOutStockRptQueryEntity();
            inOutStockRptDetailQueryEntity.OWNER_ID = "";
            inOutStockRptDetailQueryEntity.SKU_ID = "";
            inOutStockRptDetailQueryEntity.STARTDATETIME = "";
            inOutStockRptDetailQueryEntity.ENDDATETIME = "";
            inOutStockRptDetailQueryEntity.RECEIPT_ID = "";
            inOutStockRptDetailQueryEntity.VENDOR_ID = "";
            inOutStockRptDetailQueryEntity.CONSIGNEE_ID = "";
            inOutStockRptDetailQueryEntity.SKU_DESCR = "";
            inOutStockRptDetailQueryEntity.ORDER_ID = "";
            inOutStockRptDetailQueryEntity.ITEM_NO_ID = "";
            inOutStockRptDetailQueryEntity.IMPORT_DECLARATIONS_ID = "";
            inOutStockRptDetailQueryEntity.EXPORT_DECLARATIONS_ID = "";
            this.BindData2gvInOutStockDetailList(inOutStockRptDetailQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity = this.GetInOutStockRptQueryEntity();
            if (int.Parse(inOutStockRptDetailQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                inOutStockRptDetailQueryEntity.CurrentPage = (int.Parse(inOutStockRptDetailQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2gvInOutStockDetailList(inOutStockRptDetailQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity = this.GetInOutStockRptQueryEntity();
            if (int.Parse(inOutStockRptDetailQueryEntity.CurrentPage) > 0)
            {
                inOutStockRptDetailQueryEntity.CurrentPage = (int.Parse(inOutStockRptDetailQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2gvInOutStockDetailList(inOutStockRptDetailQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity = this.GetInOutStockRptQueryEntity();
            this.BindData2gvInOutStockDetailList(inOutStockRptDetailQueryEntity);
        }

        private InOutStockRptDetailQueryEntity GetInOutStockRptQueryEntity()
        {
            return (base.PageQueryEntitySession as InOutStockRptDetailQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity = this.GetInOutStockRptQueryEntity();
                inOutStockRptDetailQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2gvInOutStockDetailList(inOutStockRptDetailQueryEntity);
            }
        }

        protected void gvInOutInvDetailRpt_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15346", this.gvInOutInvDetailRpt);
        }

        protected void gvInOutInvDetailRpt_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
            }
            InOutStockRptDetailQueryEntity inOutStockRptQuery = this.GetInOutStockRptQueryEntity();
        }

        protected void gvInOutInvDetailRpt_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity = new InOutStockRptDetailQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    inOutStockRptDetailQueryEntity.OWNER_ID = this.PreviousPage.OwnerID;
                    inOutStockRptDetailQueryEntity.SKU_ID = this.PreviousPage.SkuID;
                    inOutStockRptDetailQueryEntity.STARTDATETIME = this.PreviousPage.StartTime;
                    inOutStockRptDetailQueryEntity.ENDDATETIME = this.PreviousPage.EndTime;
                    inOutStockRptDetailQueryEntity.RECEIPT_ID = this.PreviousPage.ReceiptID;
                    inOutStockRptDetailQueryEntity.VENDOR_ID = this.PreviousPage.VendorID;
                    inOutStockRptDetailQueryEntity.CONSIGNEE_ID = this.PreviousPage.ConsigneeID;
                    inOutStockRptDetailQueryEntity.SKU_DESCR = this.PreviousPage.Descr;
                    inOutStockRptDetailQueryEntity.ORDER_ID = this.PreviousPage.OrderID;
                    inOutStockRptDetailQueryEntity.ITEM_NO_ID = this.PreviousPage.ItemNoID;
                    inOutStockRptDetailQueryEntity.IMPORT_DECLARATIONS_ID = this.PreviousPage.ImportDeclarationID;
                    inOutStockRptDetailQueryEntity.EXPORT_DECLARATIONS_ID = this.PreviousPage.ExportDeclarationID;
                }
                else
                {
                    inOutStockRptDetailQueryEntity.OWNER_ID = "";
                    inOutStockRptDetailQueryEntity.SKU_ID = "";
                    inOutStockRptDetailQueryEntity.STARTDATETIME = "";
                    inOutStockRptDetailQueryEntity.ENDDATETIME = "";
                    inOutStockRptDetailQueryEntity.RECEIPT_ID = "";
                    inOutStockRptDetailQueryEntity.VENDOR_ID = "";
                    inOutStockRptDetailQueryEntity.CONSIGNEE_ID = "";
                    inOutStockRptDetailQueryEntity.SKU_DESCR = "";
                    inOutStockRptDetailQueryEntity.ORDER_ID = "";
                    inOutStockRptDetailQueryEntity.ITEM_NO_ID = "";
                    inOutStockRptDetailQueryEntity.IMPORT_DECLARATIONS_ID = "";
                    inOutStockRptDetailQueryEntity.EXPORT_DECLARATIONS_ID = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            inOutStockRptDetailQueryEntity.IsGetAll = false;
            inOutStockRptDetailQueryEntity.CurrentPage = "0";
            inOutStockRptDetailQueryEntity.PageSize = this.gvInOutInvDetailRpt.PageSize.ToString();
            this.PagerFooter.PageSize = this.gvInOutInvDetailRpt.PageSize;
            this.SetInOutStockDetailRptQueryEntity(inOutStockRptDetailQueryEntity);
            this.BindData2gvInOutStockDetailList(inOutStockRptDetailQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvInOutInvDetailRpt);
                this.LoadData();
            }
        }

        private DataSet SetExportData(DataSet ds, InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity)
        {
            string currFlagOwner = string.Empty;
            string lastFlagOwner = string.Empty;
            string currFlagReceipt = string.Empty;
            string lastFlagReceipt = string.Empty;
            try
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    currFlagOwner = ds.Tables[0].Rows[i]["OWNER_ID"].ToString() + ds.Tables[0].Rows[i]["VENDOR_ID"].ToString() + ds.Tables[0].Rows[i]["SKU_ID"].ToString();
                    currFlagReceipt = ds.Tables[0].Rows[i]["RECEIPT_ID"].ToString() + ds.Tables[0].Rows[i]["RECEIPT_DATE"].ToString() + ds.Tables[0].Rows[i]["INQTY"].ToString() + ds.Tables[0].Rows[i]["ITEM_NO_ID"].ToString();
                    if (currFlagOwner.ToUpper().Trim() == lastFlagOwner.ToUpper().Trim())
                    {
                        if (currFlagReceipt.ToUpper().Trim() == lastFlagReceipt.ToUpper().Trim())
                        {
                            ds.Tables[0].Rows[i]["RECEIPT_ID"] = "";
                            ds.Tables[0].Rows[i]["EXTERNAL_RECEIPT_ID"] = "";
                            ds.Tables[0].Rows[i]["RECEIPT_DATE"] = "";
                            ds.Tables[0].Rows[i]["INQTY"] = 0;
                            ds.Tables[0].Rows[i]["ITEM_NO_ID"] = "";
                            ds.Tables[0].Rows[i]["ITEM_NO_UOM"] = "";
                        }
                        ds.Tables[0].Rows[i]["OWNER_ID"] = "";
                        ds.Tables[0].Rows[i]["VENDOR_ID"] = "";
                        ds.Tables[0].Rows[i]["SKU_ID"] = "";
                        ds.Tables[0].Rows[i]["DESCR"] = "";
                        ds.Tables[0].Rows[i]["CUSTOM_SKU_NO"] = "";
                    }
                    lastFlagReceipt = currFlagReceipt;
                    lastFlagOwner = currFlagOwner;
                }
            }
            catch
            {
                return null;
            }
            return ds;
        }

        private DataSet SetExportDataTable2(DataSet ds, InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity)
        {
            string currFlagOwner = string.Empty;
            string lastFlagOwner = string.Empty;
            string currFlagReceipt = string.Empty;
            string lastFlagReceipt = string.Empty;
            try
            {
                for (int i = 0; i < ds.Tables[2].Rows.Count; i++)
                {
                    currFlagOwner = ds.Tables[2].Rows[i]["OWNER_ID"].ToString() + ds.Tables[2].Rows[i]["VENDOR_ID"].ToString() + ds.Tables[2].Rows[i]["SKU_ID"].ToString();
                    currFlagReceipt = ds.Tables[2].Rows[i]["RECEIPT_ID"].ToString() + ds.Tables[2].Rows[i]["RECEIPT_DATE"].ToString() + ds.Tables[2].Rows[i]["INQTY"].ToString() + ds.Tables[2].Rows[i]["ITEM_NO_ID"].ToString();
                    if (currFlagOwner.ToUpper().Trim() == lastFlagOwner.ToUpper().Trim())
                    {
                        if (currFlagReceipt.ToUpper().Trim() == lastFlagReceipt.ToUpper().Trim())
                        {
                            if (string.IsNullOrEmpty(ds.Tables[2].Rows[i - 1]["ORDER_ID"].ToString()) && (decimal.Parse(ds.Tables[2].Rows[i - 1]["OTQTY"].ToString()) <= 0M))
                            {
                                ds.Tables[2].Rows.RemoveAt(i - 1);
                                i--;
                                lastFlagReceipt = "";
                                this.deleteCount++;
                            }
                            else
                            {
                                ds.Tables[2].Rows[i]["RECEIPT_ID"] = "";
                                ds.Tables[2].Rows[i]["EXTERNAL_RECEIPT_ID"] = "";
                                ds.Tables[2].Rows[i]["RECEIPT_DATE"] = "";
                                ds.Tables[2].Rows[i]["INQTY"] = 0;
                                ds.Tables[2].Rows[i]["ITEM_NO_ID"] = "";
                                ds.Tables[2].Rows[i]["ITEM_NO_UOM"] = "";
                            }
                        }
                        if (!string.IsNullOrEmpty(lastFlagReceipt))
                        {
                            ds.Tables[2].Rows[i]["OWNER_ID"] = "";
                            ds.Tables[2].Rows[i]["VENDOR_ID"] = "";
                            ds.Tables[2].Rows[i]["SKU_ID"] = "";
                            ds.Tables[2].Rows[i]["DESCR"] = "";
                            ds.Tables[2].Rows[i]["CUSTOM_SKU_NO"] = "";
                        }
                    }
                    lastFlagReceipt = currFlagReceipt;
                    lastFlagOwner = currFlagOwner;
                }
            }
            catch
            {
                return null;
            }
            return ds;
        }

        public void SetInOutStockDetailRptQueryEntity(InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity)
        {
            base.PageQueryEntitySession = inOutStockRptDetailQueryEntity;
        }

        public InOutInvDetail2RptQuery PreviousPage
        {
            get
            {
                return (InOutInvDetail2RptQuery) base.PreviousPage;
            }
        }
    }
}

